/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
import game.interfaces.AgentInterface;

/**
 *@author Gary Doyle, Daniel Collins
 * 
 *Class for the computer agent object. Contains constructor, betting and IO methods.
 */
public class ComputerAgent implements AgentInterface{
	
    
    DictionaryTable dt = new DictionaryTable("trimmedList.txt");
    EnglishScrabbleScorer myScorer = new EnglishScrabbleScorer();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String easy = "easy", medium = "medium", hard = "hard";
    boolean firstAsk = true;
	TileRack rack;
	String currentWord;
    
    public static int cBet,cScore;
    public int bankRoll, riskTolerance, moodiness, mood, handValue,counter,agentCount = 0,difficultyInt;
    String word, bestWord,difficulty;
            
    /*
     * Constructor for the computer agent, which needs mood/risk tolerance.
     * @param The agen't initial bankroll, risk tolerance, mood.
     */
    public ComputerAgent(int br, int rt, int moo,int count){ //throws IOException {
         
    	counter = count;
        bankRoll = br;
        riskTolerance = rt;
        moodiness = moo;
        
        mood = (int) (Math.random()*moodiness);
        rack = new TileRack();
        
        
    }

    public ComputerAgent(ComputerAgent ca){
    	this.counter = ca.counter;
    	this.bankRoll = ca.bankRoll;
    	this.riskTolerance = ca.riskTolerance;
    	this.moodiness = ca.moodiness;
    	this.word = ca.word;
    	this.mood = ca.mood;
    	this.rack = ca.rack;
    }
        
    public ComputerAgent(PlayerAgent pa){
    	this.bankRoll = pa.bankRoll;
    	this.cScore = pa.pScore;
    	this.rack = pa.rack;
    	
    	counter = 0;
        riskTolerance = 0;
        moodiness = 0;
        word = " ";
        mood = 0;
     }
    
    
    
    /*
     * Gets the input for the difficulty the player wants to play on.
     * @param none.
     */
    public int getDifficulty() throws IOException{
    	if (firstAsk){
    	difficulty = JOptionPane.showInputDialog(null, "Which difficulty do you want to play the game on? \nPlease enter: easy/medium/hard");
    	}
    	try {
    		difficulty = difficulty.toLowerCase();
                        
    		 if(difficulty.equals(easy)) difficultyInt = 3;
            else if(difficulty.equals(medium)) difficultyInt = 6;
            else if(difficulty.equals(hard)) difficultyInt = 8;
    		
            else if (!(difficulty.equals(easy))||!(difficulty.equals(medium))||!(difficulty.equals(hard))){
            	firstAsk = false;
                System.out.println("You didn't enter a valid difficulty. Please try again.");
                getDifficulty();
            }
            
            
    		           
        } catch (NumberFormatException nFE) {
        	System.out.println("That wasn't a valid number! Please try again!");
            getDifficulty();
        }
    	return difficultyInt;
    }
    
    /*
     * Returns the best possible word from the letters of the length determined by the difficulty. 
     * @param The agen't initial bankroll.
     */
    public String bestWord(){
    	
        return word = dt.getBestWordIn(difficultyInt, rack.returnString());
    }
    
    /*
     * Returns the score of the word generated by bestWord.
     * @param none.
     */
    public int getScore(){

        return handValue = myScorer.StringScore(bestWord());
    }

    /*
     * Takes the amount from the agent's bankroll.
     * @param the amount to be subtracted.
     */
    public void takeFromBankRoll(int amount) {
    	bankRoll = (bankRoll - amount);  
    }
    

    /*
     * Adds the amount to the agent's bankroll.
     * @param the amount to be added.
     */
    public void addToBankRoll(int amount) {
         bankRoll = (bankRoll + amount);
    }
    
    public int incrementCounter(){
    	counter = counter +1;
    	if (counter == 4) counter = 0;
    	return counter;
    }

    public boolean isDealer(){
    	return counter == 0;
    }
    
    public boolean isLittleBlind() {
        return counter == 1;
    }

    
    public boolean isBigBlind() {
        return counter == 2;
    }

    /*
     * Checks if the agent is bankrupt.
     * @param none.
     */
    @Override
    public boolean isBankrupt() {
        return bankRoll == 0;
    }

    /*
     * Returns the agent's bankroll.
     * @param none.
     */
    @Override
    public int getCurrentBankRoll() {
        return bankRoll;
    }
    
    /*
     * Checks if the agent should fold.
     * @param none.
     */
    public boolean shouldFold() {
        int rand = (int) (Math.random() * 100);
        return (rand > riskTolerance + handValue); //implemented as per spec
     }
    
    /*
     * Checks if the agent should raise.
     * @param none.
     */
    public boolean shouldRaise() {
        int rand = (int) (Math.random() * 100);
        
        
        return (rand < (riskTolerance + mood + handValue)); //implemented as per spec
     }
    
    /*
     * Checks if the agent should check.
     * @param none.
     */
    public boolean shouldCheck() {
        return (shouldFold() == shouldRaise());
     }
    
    /*
     * Testing method. Not working.
     * @param none.
     */
    public boolean makeDecision(){//boolean c,boolean f,boolean r){
        boolean c = shouldCheck();
        boolean f = shouldFold();
        boolean r = shouldRaise();
        
        if (f&&r){
            System.out.println("Player will check");
            return c;
        }
        else if (f){
            System.out.println("Player will fold");
            return f;
        }
        else{
            System.out.println("Player will raise");
            return r;
        }
         
    }

 

    
}
